<template>
  <ul class="todo-main">
    <Item v-for="todo in todoList" :key="todo.id" v-bind="todo" />
  </ul>
</template>

<script>
import { v4 as uuidv4 } from "uuid";
import Item from "./Item";
export default {
  name: "List",
  data() {
    return {
      todoList: [
        { id: uuidv4(), thing: "唱", done: false },
        { id: uuidv4(), thing: "跳", done: true },
        { id: uuidv4(), thing: "rap", done: false },
      ],
    };
  },
  components: {
    Item,
  },
  mounted() {
    //给$bus绑定一个接受header组件传值的事件newTodo
    this.$bus.$on("newTodo", (newTodo) => {
      this.todoList.unshift(newTodo);
    });

    //注意 因为绑定和调用都在mounted里,目前可能会有问题,未来肯定没有问题,因为未来调用事件传值,肯定是在ajax异步请求数据回来之后,才传值的
    //调用$bus上的getTodos事件,给footer传递todolist
    this.$bus.$emit("getTodos", this.todoList);
  },
  watch: {
    todoList: {
      deep: true,
      handler() {
        this.$bus.$emit("getTodos", this.todoList);
      },
    },
  },
};
</script>

<style scoped>
/*main*/
.todo-main {
  margin-left: 0px;
  border: 1px solid #ddd;
  border-radius: 2px;
  padding: 0px;
}

.todo-empty {
  height: 40px;
  line-height: 40px;
  border: 1px solid #ddd;
  border-radius: 2px;
  padding-left: 5px;
  margin-top: 10px;
}
</style>